package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.InternalCommunication;
import com.amsp.oa.oademo.mapper.InternalCommunicationMapper;
import com.amsp.oa.oademo.service.InternalCommunicationService;
import com.amsp.oa.oademo.vo.InternalCommunicationVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 内部联络单Service实现类
 */
@Service
public class InternalCommunicationServiceImpl extends ServiceImpl<InternalCommunicationMapper, InternalCommunication>
        implements InternalCommunicationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateInternalCommunication(InternalCommunicationVO vo) {
        InternalCommunication entity = new InternalCommunication();
        BeanUtils.copyProperties(vo, entity);

        if (entity.getId() == null) {
            entity.setCreateTime(LocalDateTime.now());
        }
        entity.setUpdateTime(LocalDateTime.now());

        return saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveOrUpdateInternalCommunication(List<InternalCommunicationVO> voList) {
        if (voList == null || voList.isEmpty()) {
            return false;
        }

        List<InternalCommunication> entityList = voList.stream().map(vo -> {
            InternalCommunication entity = new InternalCommunication();
            BeanUtils.copyProperties(vo, entity);

            if (entity.getId() == null) {
                entity.setCreateTime(LocalDateTime.now());
            }
            entity.setUpdateTime(LocalDateTime.now());

            return entity;
        }).collect(Collectors.toList());

        return saveOrUpdateBatch(entityList);
    }

    @Override
    public InternalCommunicationVO getInternalCommunicationDetail(Long id) {
        if (id == null) {
            return null;
        }

        InternalCommunication entity = getById(id);
        if (entity == null) {
            return null;
        }

        InternalCommunicationVO vo = new InternalCommunicationVO();
        BeanUtils.copyProperties(entity, vo);

        return vo;
    }

    @Override
    public Page<InternalCommunicationVO> getInternalCommunicationPage(Page<InternalCommunication> page,
            InternalCommunicationVO vo) {
        LambdaQueryWrapper<InternalCommunication> queryWrapper = new LambdaQueryWrapper<>();

        // 构建查询条件
        if (vo != null) {
            if (StringUtils.hasText(vo.getDocNumber())) {
                queryWrapper.like(InternalCommunication::getDocNumber, vo.getDocNumber());
            }

            if (StringUtils.hasText(vo.getRecipientUnit())) {
                queryWrapper.like(InternalCommunication::getRecipientUnit, vo.getRecipientUnit());
            }

            if (StringUtils.hasText(vo.getRecipient())) {
                queryWrapper.like(InternalCommunication::getRecipient, vo.getRecipient());
            }

            if (StringUtils.hasText(vo.getSubject())) {
                queryWrapper.like(InternalCommunication::getSubject, vo.getSubject());
            }

            if (StringUtils.hasText(vo.getIssuingDepartment())) {
                queryWrapper.like(InternalCommunication::getIssuingDepartment, vo.getIssuingDepartment());
            }
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(InternalCommunication::getCreateTime);

        // 执行分页查询
        Page<InternalCommunication> entityPage = page(page, queryWrapper);

        // 转换为VO对象
        Page<InternalCommunicationVO> voPage = new Page<>();
        voPage.setCurrent(entityPage.getCurrent());
        voPage.setSize(entityPage.getSize());
        voPage.setTotal(entityPage.getTotal());

        List<InternalCommunicationVO> voList = new ArrayList<>();
        for (InternalCommunication entity : entityPage.getRecords()) {
            InternalCommunicationVO communicationVO = new InternalCommunicationVO();
            BeanUtils.copyProperties(entity, communicationVO);
            voList.add(communicationVO);
        }

        voPage.setRecords(voList);

        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteInternalCommunication(Long id) {
        if (id == null) {
            return false;
        }

        return removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteInternalCommunication(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }

        return removeByIds(ids);
    }
}